//
//  FZOriginView.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/30.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class FZOriginView: UIView {

    var statusOriginModel : FZStatusViewModel?{
        
        didSet {
            
            let userModel : FZUserModel = (statusOriginModel?.statusModel?.user )!
            
            headImg.sd_setImage(with:URL.init(string: userModel.profile_image_url!) , placeholderImage: UIImage(named: "avatar_default_big"))
            screenNameLab.text = userModel.screen_name
            verifiedType.image = statusOriginModel?.verifiedTypeImg
            mbankImg.image = statusOriginModel?.mbrankImg
            pictureView.statusPictureModel = statusOriginModel?.statusModel?.pic_urls
            source.text = statusOriginModel?.sourceTxt
            createdAt.text = statusOriginModel?.createTime
            
            weiboText.attributedText = statusOriginModel?.originAttributeString
            
            if let picData = statusOriginModel?.statusModel {
                
                judgeContrain(statusModel: picData)
            }
        }
    }
    
    var viewConstraint: Constraint?
    //配图
    fileprivate lazy var pictureView : FZPictureView = FZPictureView()
    //头像
    fileprivate lazy var headImg :UIImageView = {
       
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        return imgView
    }()
    //认证等级
    fileprivate lazy var verifiedType :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    fileprivate lazy var screenNameLab : UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 15)
        lab.text = "D大大"
        lab.textColor = UIColor.red
        return lab
    }()
    //会员等级
    fileprivate lazy var mbankImg : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //时间
    fileprivate lazy var createdAt : UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.text = "昨天 12:24"
        lab.textColor = UIColor.orange
        return lab
    }()
    
    //来源
    fileprivate lazy var source : UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.text = "来自微博"
        lab.textColor = UIColor.darkGray
        return lab
    }()
    
    //微博内容
    fileprivate lazy var weiboText : UILabel = {
       let webText = UILabel()
       webText.font = UIFont.systemFont(ofSize: 14)
       webText.textColor = UIColor.darkGray
       webText.text = "我热爱学习,学习使我快乐我热爱学习,学习使我快乐我热爱学习,学习使我快乐我热爱学习,学习使我快乐我热爱学习,学习使我快乐我热爱学习,学习使我快乐我热爱学习,学习使我快乐"
       webText.numberOfLines = 0
       return webText
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        addSubview(headImg)
        addSubview(verifiedType)
        addSubview(screenNameLab)
        addSubview(mbankImg)
        addSubview(createdAt)
        addSubview(source)
        addSubview(weiboText)
        addSubview(pictureView)
        setupUI()
    }
    
    func judgeContrain(statusModel : FZStatusModel) {
        
        viewConstraint?.deactivate()
        
        if statusModel.pic_urls?.count == 0 {
            
            self.snp.makeConstraints({ (make) in
                viewConstraint = make.bottom.equalTo(weiboText.snp.bottom).offset(10).constraint
            })
           pictureView.isHidden = true
            
        }else{
            
            self.snp.makeConstraints({ (make) in
                
                viewConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(10).constraint
                
            })
            pictureView.isHidden = false
        }

    }
    
    func setupUI() {
        
       headImg.snp.makeConstraints { (make) in
       make.top.equalTo(self).offset(10)
       make.left.equalTo(self).offset(10)
       make.size.equalTo(CGSize(width: 40, height: 40))
        
       verifiedType.snp.makeConstraints({ (make) in
          make.right.equalTo(headImg.snp.right).offset(9)
          make.bottom.equalTo(headImg.snp.bottom).offset(9)
       })
        
       screenNameLab.snp.makeConstraints({ (make) in
         make.top.equalTo(headImg.snp.top)
         make.left.equalTo(headImg.snp.right).offset(10)
        
       })
        
       mbankImg.snp.makeConstraints({ (make) in
        make.top.equalTo(screenNameLab.snp.top)
        make.left.equalTo(screenNameLab.snp.right).offset(10)
        
       })
        
       createdAt.snp.makeConstraints({ (make) in
         make.left.equalTo(screenNameLab.snp.left)
         make.bottom.equalTo(headImg.snp.bottom)
       })
        
       source.snp.makeConstraints({ (make) in
         make.bottom.equalTo(createdAt.snp.bottom)
         make.left.equalTo(createdAt.snp.right).offset(10)
       })
        
       weiboText.snp.makeConstraints({ (make) in
        
         make.top.equalTo(headImg.snp.bottom).offset(15)
         make.leading.equalTo(headImg)
         make.right.equalTo(self).offset(-10)
        
       })
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(weiboText.snp.bottom).offset(10)
            make.left.equalTo(weiboText.snp.left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }

       self.snp.makeConstraints({ (make) in
        viewConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(10).constraint
        
       })
    }
        
 }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}











